project/mdnsd.git
2 months agocache: improve update call by doing a full refresh probe master
John Crispin [Tue, 17 Sep 2024 16:00:37 +0000 (18:00 +0200)]
cache: improve update call by doing a full refresh probe

Signed-off-by: John Crispin <john@phrozen.org>
2 months agodns: reply to A/AAAA questions for additional hostnames
John Crispin [Sun, 21 Apr 2024 17:44:07 +0000 (19:44 +0200)]
dns: reply to A/AAAA questions for additional hostnames

Signed-off-by: John Crispin <john@phrozen.org>
10 months agoubus: assume that the service iface can be NULL
Felix Fietkau [Mon, 8 Jan 2024 09:21:48 +0000 (10:21 +0100)]
ubus: assume that the service iface can be NULL

Fix crash on dump

Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 months agointerface: remove unused peer field
Felix Fietkau [Mon, 8 Jan 2024 09:00:06 +0000 (10:00 +0100)]
interface: remove unused peer field

Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 months agointerface: add missing cache cleanup on interface free
Felix Fietkau [Fri, 5 Jan 2024 21:13:02 +0000 (22:13 +0100)]
interface: add missing cache cleanup on interface free

Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 months agoadd the ability to announce additional hostnames
John Crispin [Mon, 27 Nov 2023 06:47:17 +0000 (07:47 +0100)]
add the ability to announce additional hostnames

Signed-off-by: John Crispin <john@phrozen.org>
12 months agoadd udebug support
Felix Fietkau [Tue, 21 Nov 2023 06:33:35 +0000 (07:33 +0100)]
add udebug support

Signed-off-by: Felix Fietkau <nbd@nbd.name>
12 months agointerface: fix interface memory corruption
Felix Fietkau [Tue, 21 Nov 2023 05:45:09 +0000 (06:45 +0100)]
interface: fix interface memory corruption

- set vlist keep_old=true, because the iface_update_cb expects it
- update ifindex on reload

Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agoubus: add array flag support for the hosts method
Felix Fietkau [Thu, 19 Oct 2023 09:26:22 +0000 (11:26 +0200)]
ubus: add array flag support for the hosts method

Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agouse hostname from SRV record to look up IP addresses
Felix Fietkau [Thu, 19 Oct 2023 08:58:04 +0000 (10:58 +0200)]
use hostname from SRV record to look up IP addresses

On many devices, the main record name contains the long form hostname,
whereas the A/AAAA records are assigned to the hostname indicated in the
SRV record.
This fixes getting IP addresses via browse for many devices

Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agocache: dump hostname target from srv records
Felix Fietkau [Thu, 19 Oct 2023 08:49:11 +0000 (10:49 +0200)]
cache: dump hostname target from srv records

This can be used to look up the correct hostname to determine the ip addresses

Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agointerface: use a global socket instead of per-interface ones
Felix Fietkau [Thu, 19 Oct 2023 08:38:29 +0000 (10:38 +0200)]
interface: use a global socket instead of per-interface ones

Fix corner cases related to having multiple addresses on a single interface

Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agoFix AVL tree traversal in cache_record_find and cache_host_is_known
Martin Röder [Wed, 18 Oct 2023 16:32:34 +0000 (18:32 +0200)]
Fix AVL tree traversal in cache_record_find and cache_host_is_known

The AVL tree traversal in both functions systematically misses the last
AVL tree element. This can lead to duplicate cache entries and lookup
failures.

The fix duplicates the correct AVL tree traversal approach of
cache_dump_recursive().

Signed-off-by: Martin Röder <mroeder@metz-connect.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agoubus: add a browse flag for suppressing cached ip addresses
Felix Fietkau [Wed, 18 Oct 2023 13:28:59 +0000 (15:28 +0200)]
ubus: add a browse flag for suppressing cached ip addresses

Reduces the response size when they are not needed, or queried separately

Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 months agocache: make record/hostname lookup case-insensitive
Felix Fietkau [Wed, 18 Oct 2023 12:44:45 +0000 (14:44 +0200)]
cache: make record/hostname lookup case-insensitive

Fixes lookup issues on hosts that announce themselves with uppercase
in their hostname

Signed-off-by: Felix Fietkau <nbd@nbd.name>
22 months agoubus: add the interface name to browse reply
John Crispin [Mon, 16 Jan 2023 10:04:30 +0000 (11:04 +0100)]
ubus: add the interface name to browse reply

Signed-off-by: John Crispin <john@phrozen.org>
22 months agoubus: also allow dumping ipv4/6 as an array
John Crispin [Sun, 15 Jan 2023 12:41:44 +0000 (13:41 +0100)]
ubus: also allow dumping ipv4/6 as an array

Signed-off-by: John Crispin <john@phrozen.org>
22 months agoubus: make it possible to return txt records as an array
John Crispin [Sun, 15 Jan 2023 10:04:00 +0000 (11:04 +0100)]
ubus: make it possible to return txt records as an array

Signed-off-by: John Crispin <john@phrozen.org>
23 months agoubus: allow filtering by service when invoking the browse method
John Crispin [Tue, 27 Dec 2022 19:24:58 +0000 (20:24 +0100)]
ubus: allow filtering by service when invoking the browse method

Signed-off-by: John Crispin <john@phrozen.org>
3 years agoreconnect: increase initial connect delay
John Crispin [Wed, 30 Jun 2021 12:15:53 +0000 (14:15 +0200)]
reconnect: increase initial connect delay

The initial reconnect will almost always fail and then retry after 1s.
Use 1s instead of 100ms.

Signed-off-by: John Crispin <john@phrozen.org>
3 years agoservice: fix compilation with GCC 10
Stijn Tintel [Thu, 13 May 2021 11:59:56 +0000 (14:59 +0300)]
service: fix compilation with GCC 10

Building with GCC 10.3.0 fails with the following error:

service.c:243:10: error: 'strncpy' offset 6 from the object at 'b' is
out of the bounds of referenced subobject 'name' with type 'uint8_t[]'
{aka 'unsigned char[]'} at offset 6 [-Werror=array-bounds]
  243 |  s->id = strncpy(d_id, blobmsg_name(b), n);
        |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix it by passing the length returned by strlen to strncpy without
adding an extra byte for the string terminator. Add the extra byte only
in the calloc_a call, which will initialize it to NULL.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
Acked-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
3 years agoumdns: fix 64-bit time format string
Rosen Penev [Thu, 15 Oct 2020 06:10:27 +0000 (23:10 -0700)]
umdns: fix 64-bit time format string

Fixes compilation under musl 1.2.0

Signed-off-by: Rosen Penev <rosenp@gmail.com>
4 years agocache: cache_answer: fix off by one
Petr Štetiar [Tue, 13 Oct 2020 11:56:47 +0000 (13:56 +0200)]
cache: cache_answer: fix off by one

Fixes following issue found by the AFL fuzzer which was then confirmed
by the libFuzzer as well:

 ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6040000072fa at pc 0x00000051f647 bp 0x7ffe95787cd0 sp 0x7ffe95787498
 READ of size 16 at 0x6040000072fa thread T0
    #0 0x51f646 in __asan_memcpy (mdnsd/build/tests/fuzz/test-fuzz+0x51f646)
    #1 0x5539d3 in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34:10
    #2 0x5539d3 in cache_answer mdnsd/cache.c:311:3
    #3 0x561c7a in parse_answer mdnsd/dns.c:345:3
    #4 0x55de9c in dns_handle_packet mdnsd/dns.c:446:7
    #5 0x55a9f4 in fuzz_dns_handle_packet mdnsd/tests/fuzz/test-fuzz.c:31:2

 0x6040000072fa is located 0 bytes to the right of 42-byte region [0x6040000072d0,0x6040000072fa)
 allocated by thread T0 here:
     #0 0x520412 in calloc (mdnsd/build/tests/fuzz/test-fuzz+0x520412)

memcpy() reads one byte past `rdata` buffer as the read starts from the
2nd byte, but the reading length wasn't adjusted to that fact.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agocache: cache_record_find: fix buffer overflow
Petr Štetiar [Mon, 12 Oct 2020 15:40:19 +0000 (17:40 +0200)]
cache: cache_record_find: fix buffer overflow

Fixes following buffer overflow:

 ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000007338b8 at pc 0x0000004db339 bp 0x7ffe370e6140 sp 0x7ffe370e6138
 READ of size 8 at 0x0000007338b8 thread T0
     #0 0x4db338 in cache_record_find mdnsd/cache.c:197:17
     #1 0x4d74b4 in cache_answer mdnsd/cache.c:336:6
     #2 0x4cf04a in parse_answer mdnsd/dns.c:343:3
     #3 0x4cb272 in dns_handle_packet mdnsd/dns.c:442:7
     #4 0x4f508c in read_socket4 mdnsd/interface.c:253:3
     #5 0x7fb81dddc73d in uloop_run_events libubox/uloop.c:198:4
     #6 0x7fb81dddc73d in uloop_run_timeout libubox/uloop.c:555:3
     #7 0x4c77cd in uloop_run libubox/uloop.h:111:9
     #8 0x4c7757 in main mdnsd/main.c:99:2

 0x0000007338b8 is located 8 bytes to the right of global variable 'records' defined in 'mdnsd/cache.c:45:1' (0x733880) of size 48
 SUMMARY: AddressSanitizer: global-buffer-overflow mdnsd/cache.c:197:17 in cache_record_find

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agocmake: tests: provide umdns-san binary
Petr Štetiar [Tue, 13 Oct 2020 12:40:14 +0000 (14:40 +0200)]
cmake: tests: provide umdns-san binary

Which is compiled with clang UB, address and leak sanitizers which is
handy for example with fuzzing directly over network.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agotests: add dns_handle_packet_file tool
Petr Štetiar [Tue, 13 Oct 2020 12:59:38 +0000 (14:59 +0200)]
tests: add dns_handle_packet_file tool

So it can read packets from file, which is handy when using for example
AFL or hongfuzz fuzzers.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agotests: add libFuzzer based fuzzing
Petr Štetiar [Tue, 13 Oct 2020 12:36:44 +0000 (14:36 +0200)]
tests: add libFuzzer based fuzzing

LibFuzzer is in-process, coverage-guided, evolutionary fuzzing engine.

LibFuzzer is linked with the library under test, and feeds fuzzed inputs
to the library via a specific fuzzing entrypoint (aka "target
function"); the fuzzer then tracks which areas of the code are reached,
and generates mutations on the corpus of input data in order to maximize
the code coverage.

So lets use libFuzzer to fuzz dns_handle_packet for the start.

Ref: https://llvm.org/docs/LibFuzzer.html
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agocmake: create static library
Petr Štetiar [Tue, 13 Oct 2020 12:23:46 +0000 (14:23 +0200)]
cmake: create static library

So it could be reused in other binaries for fuzzing etc.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agointerface: fix possible null pointer dereference
Petr Štetiar [Tue, 13 Oct 2020 13:26:01 +0000 (15:26 +0200)]
interface: fix possible null pointer dereference

Fixes following issue reported by clang-12 static analyzer:

 mdnsd/interface.c:250:6: warning: Access to field 'ipi_ifindex' results in a dereference of a null pointer (loaded from variable 'inp')
         if (inp->ipi_ifindex != iface->ifindex)
             ^~~~~~~~~~~~~~~~

 mdnsd/interface.c:323:6: warning: Access to field 'ipi6_ifindex' results in a dereference of a null pointer (loaded from variable 'inp')
         if (inp->ipi6_ifindex != iface->ifindex)
             ^~~~~~~~~~~~~~~~~

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agointerface: fix value stored to 'fd' is never read
Petr Štetiar [Tue, 13 Oct 2020 13:18:44 +0000 (15:18 +0200)]
interface: fix value stored to 'fd' is never read

Fixes following issue reported by clang-12 static analyzer:

 mdnsd/interface.c:360:3: warning: Value stored to 'fd' is never read
                 fd = -1;
                 ^    ~~
 mdnsd/interface.c:394:3: warning: Value stored to 'fd' is never read
                 fd = -1;
                 ^    ~~

Original author most likely wanted to assing -1 to the original
interface filedescriptor.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoAdd initial GitLab CI support
Petr Štetiar [Tue, 1 Oct 2019 19:50:54 +0000 (21:50 +0200)]
Add initial GitLab CI support

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agocmake: fix include dirs and libs lookup
Petr Štetiar [Thu, 26 Sep 2019 04:29:52 +0000 (06:29 +0200)]
cmake: fix include dirs and libs lookup

In order to make it compile properly in more environments.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoservice.c: fix build on gcc 10
Kevin Darbyshire-Bryant [Sun, 19 Jul 2020 13:14:12 +0000 (14:14 +0100)]
service.c: fix build on gcc 10

Resolve error:
/Users/kevin/wrt/build_dir/target-x86_64_musl/umdns-2020-06-08-d13290b4/service.c: In function 'service_load_blob':
/Users/kevin/wrt/build_dir/target-x86_64_musl/umdns-2020-06-08-d13290b4/service.c:240:10: error: 'strcpy' offset 6 from the object at 'b' is out of the bounds of referenced subobject 'name' with type 'uint8_t[]' {aka 'unsigned char[]'} at offset 6 [-Werror=array-bounds]
  240 |  s->id = strcpy(d_id, blobmsg_name(b));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Users/kevin/wrt/staging_dir/target-x86_64_musl/usr/include/libubus.h:23,
                 from /Users/kevin/wrt/build_dir/target-x86_64_musl/umdns-2020-06-08-d13290b4/service.c:23:
/Users/kevin/wrt/staging_dir/target-x86_64_musl/usr/include/libubox/blobmsg.h:42:10: note: subobject 'name' declared here
   42 |  uint8_t name[];
      |          ^~~~
cc1: all warnings being treated as errors
make[5]: *** [CMakeFiles/umdns.dir/build.make:132: CMakeFiles/umdns.dir/service.c.o] Error 1

Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
4 years agoFix advertised IPv6 addresses
David Woodhouse [Mon, 8 Jun 2020 18:50:38 +0000 (19:50 +0100)]
Fix advertised IPv6 addresses

The dns_reply_a() function appears to filter out IPv6 addresses other
than link-local. This means that advertised services are unreachable
over IPv6 (since on the client side, we drop the interface identifier
so link-local addresses aren't usable anyway).

Fix it to include all addresses, in accordance with RFC6762 §6.2:

   When a Multicast DNS responder sends a Multicast DNS response message
   containing its own address records, it MUST include all addresses
   that are valid on the interface on which it is sending the message,
   and MUST NOT include addresses that are not valid on that interface
   (such as addresses that may be configured on the host's other
   interfaces).  For example, if an interface has both an IPv6 link-
   local and an IPv6 routable address, both should be included in the
   response message so that queriers receive both and can make their own
   choice about which to use.  This allows a querier that only has an
   IPv6 link-local address to connect to the link-local address, and a
   different querier that has an IPv6 routable address to connect to the
   IPv6 routable address instead.

4 years agodns.c: fix input validation fix
Kevin Darbyshire-Bryant [Fri, 24 Apr 2020 10:01:12 +0000 (11:01 +0100)]
dns.c: fix input validation fix

Thanks to Guido Vranken <guido@guidovranken.com> for pointing out I'd
forgotten to de-ref a pointer in the previous fix.

Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
4 years agodns.c: improve input validation
Kevin Darbyshire-Bryant [Sun, 12 Apr 2020 16:53:05 +0000 (17:53 +0100)]
dns.c: improve input validation

dns.c scan_name() add more input validation
parse_answer() add remaining length check
dns_handle_packet() add remaining length check

Addresses CVE-2020-11750

Thanks to Guido Vranken <guido@guidovranken.com> for the report who
requested credit be given to 'ForAllSecure Mayhem'.

Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
4 years agoumdns: fix unused error
Polynomialdivision [Sun, 5 Apr 2020 07:20:12 +0000 (09:20 +0200)]
umdns: fix unused error

Umdns does not compile on target-arm_cortex-a15+neon-vfpv4_musl_eabi.
Add the ttl variable to debug output to supress unused warning that
leads to an error.

Signed-off-by: Nick Hainke <vincent@systemli.org>
4 years agodns: explicitly endian-convert all fields in header and question
Paul Fertser [Sat, 4 Apr 2020 10:25:14 +0000 (13:25 +0300)]
dns: explicitly endian-convert all fields in header and question

For added type-safety and to avoid possible alignment issues make all
conversions explicit. While at it, mark header struct as packed so that
its layout is guaranteed to match RFC.

Prompted by gcc 8 & 9 warning in dns.c:

dns.c:261:2: error: converting a packed ‘struct dns_question’
pointer (alignment 1) to a ‘uint16_t’ {aka ‘short unsigned int’} pointer
(alignment 2) may result in an unaligned pointer value
[-Werror=address-of-packed-member]

261 |  uint16_t *swap = (uint16_t *) q;

Signed-off-by: Paul Fertser <fercerpav@gmail.com>
[Tweak commit message]
Acked-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
6 years agoumdnsd: Replace strerror(errno) with %m.
Rosen Penev [Mon, 25 Dec 2017 22:59:00 +0000 (14:59 -0800)]
umdnsd: Replace strerror(errno) with %m.

Saves 352 bytes under glibc. No functional difference.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
7 years agoAdd debug output for service_timeout
Philipp Meier [Thu, 28 Sep 2017 07:09:39 +0000 (09:09 +0200)]
Add debug output for service_timeout

Signed-off-by: Philipp Meier <philipp.meier@neratec.com>
7 years agoRemove incorrect comma in http service json config
Philipp Meier [Thu, 28 Sep 2017 07:06:36 +0000 (09:06 +0200)]
Remove incorrect comma in http service json config

Signed-off-by: Philipp Meier <philipp.meier@neratec.com>
7 years agoRemove ttl==255 restriction for queries
Philipp Meier [Thu, 28 Sep 2017 07:09:03 +0000 (09:09 +0200)]
Remove ttl==255 restriction for queries

Signed-off-by: Philipp Meier <philipp.meier@neratec.com>
7 years agoSupport specifying instance name in JSON file
Rafał Miłecki [Mon, 22 May 2017 09:57:47 +0000 (11:57 +0200)]
Support specifying instance name in JSON file

So far we were using host label as the instance name for every service.
This change allows specifying it manually and fallbacks to the label for
backward compatibility.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoSupport PTR queries for a specific service
Rafał Miłecki [Wed, 10 May 2017 20:45:49 +0000 (22:45 +0200)]
Support PTR queries for a specific service

We should check if queried name starts with a _ to see if it is about a
specific service + domain.

Fixes: 70c66fbbcde86 ("Fix sending replies to PTR questions")
Reported-by: Cristian Morales Vega <cristian@samknows.com>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoAllow filtering with instance name in service_reply
Rafał Miłecki [Wed, 10 May 2017 20:45:48 +0000 (22:45 +0200)]
Allow filtering with instance name in service_reply

This will allow sending replies more flexibly.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoStore instance name in the struct service
Rafał Miłecki [Wed, 10 May 2017 20:45:47 +0000 (22:45 +0200)]
Store instance name in the struct service

This will allow using custom instace names in the future. Right now we
always set it to the host-specific label so there is no behavior change
yet with this patch.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoRename service_name function to the service_instance_name
Rafał Miłecki [Wed, 10 May 2017 10:47:33 +0000 (12:47 +0200)]
Rename service_name function to the service_instance_name

This name matches what is really returned by the function according to
the RFC 6763. Also document it while at it.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoRename mdns_hostname variable to the umdns_host_label
Rafał Miłecki [Wed, 10 May 2017 10:47:32 +0000 (12:47 +0200)]
Rename mdns_hostname variable to the umdns_host_label

In the whole RFC 6762 document "host name" means a fully qualified
domain name. The value we store in this variable is just a first label
of the name so rename it properly to make the code just a bit easier to
follow.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoFix sending unicast questions on cache expire
Rafał Miłecki [Mon, 20 Mar 2017 23:00:16 +0000 (00:00 +0100)]
Fix sending unicast questions on cache expire

Sending unicast questions requires passing IP address. Pass the one that
was cached when caching DNS record.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoKeep source sockaddr for every cached DNS record
Rafał Miłecki [Mon, 20 Mar 2017 23:00:15 +0000 (00:00 +0100)]
Keep source sockaddr for every cached DNS record

This will be required for sending questions on TTL timeouts. When that
happens we want to send unicast question but it's currently broken as we
don't know original IP address.

This change stores whole sockaddr (using struct sockaddr_storage). In
theory it'd be sufficient to store struct in_addr or struct in6_addr but
1) There isn't helper struct for storing IP version agnostic address
2) interface_send_packet already expects struct sockaddr

It hopefully won't hurt memory usage that bad to store a bit more info.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoFix code freeing cached non-A(AAA) records too early
Rafał Miłecki [Mon, 20 Mar 2017 16:55:50 +0000 (17:55 +0100)]
Fix code freeing cached non-A(AAA) records too early

Fixes: f89986b67dd5 ("Fix refreshing cached A(AAA) records that expire")
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoFix replying to "QU" questions received on unicast interface
Rafał Miłecki [Mon, 20 Mar 2017 16:35:21 +0000 (17:35 +0100)]
Fix replying to "QU" questions received on unicast interface

We may receive questions on unicast interface, e.g. when another client
is refreshing its entries. "QU" questions are "questions requesting
unicast responses" which means we should obviously send unicast replies.

Old code wasn't setting "to" variable correctly so IP address wasn't
passed back to the interface layer. It means replies were never sent.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoFix reading port of incoming packets
Rafał Miłecki [Mon, 20 Mar 2017 16:08:11 +0000 (17:08 +0100)]
Fix reading port of incoming packets

We weren't conerting values from network byte order which could result
in rejecting packets in dns_handle_packet.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoUse MCAST_PORT define for port 5353
Rafał Miłecki [Mon, 20 Mar 2017 16:05:42 +0000 (17:05 +0100)]
Use MCAST_PORT define for port 5353

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoUse one define for DNS-Based Service Discovery service name
Rafał Miłecki [Mon, 20 Mar 2017 12:59:04 +0000 (13:59 +0100)]
Use one define for DNS-Based Service Discovery service name

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoDrop entries cached for interface we're going to delete
Rafał Miłecki [Mon, 20 Mar 2017 11:54:50 +0000 (12:54 +0100)]
Drop entries cached for interface we're going to delete

Since we free memory with struct interface we can't keep
referencing/using it in cached records or services.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoFix comment typo in cache_gc_timer
Rafał Miłecki [Mon, 20 Mar 2017 11:34:59 +0000 (12:34 +0100)]
Fix comment typo in cache_gc_timer

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoFix refreshing cached A(AAA) records that expire
Rafał Miłecki [Mon, 20 Mar 2017 10:45:07 +0000 (11:45 +0100)]
Fix refreshing cached A(AAA) records that expire

Old code was trying to refresh record after it has already expired. Now
it tries after 50% of TTL has passed - we do the same for other records.

Fixes: 80dd24602480 ("Refresh DNS records A and AAAA directly")
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoDon't cache hosts as services
Rafał Miłecki [Fri, 10 Mar 2017 07:59:23 +0000 (08:59 +0100)]
Don't cache hosts as services

This was kind of abuse of struct cache_service and the tree storing all
cached entries. Host isn't really a service, treating it so required
extra checks whenever dealing with cached entries.

Now we converted all code to read DNS records A and AAAA directly we
don't need to store these faked services.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoRefresh DNS records A and AAAA directly
Rafał Miłecki [Fri, 10 Mar 2017 07:59:22 +0000 (08:59 +0100)]
Refresh DNS records A and AAAA directly

So far records A and AAAA were being connected and stored as a single
*service*. It's possible to handle these records directly without this
service trick and it also provides better control over them (some corner
cases like different TTL values).

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoAccess cached records (instead of services) to read list of hosts
Rafał Miłecki [Fri, 10 Mar 2017 07:59:21 +0000 (08:59 +0100)]
Access cached records (instead of services) to read list of hosts

We don't need to access hosts stored as services. It's enough to look
for cached A and AAAA DNS records.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoCancel (re)connect timer when deleting interface
Rafał Miłecki [Fri, 10 Mar 2017 10:49:44 +0000 (11:49 +0100)]
Cancel (re)connect timer when deleting interface

Firing timeout would cause a crash if interface has been deleted
meanwhile. The easiest way to trigger this was calling set_config ubus
method twice within 100 ms.
Cancel pending interface start before freeing it.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoDrop unneeded code to simplify getting hosts over ubus
Rafał Miłecki [Thu, 9 Mar 2017 21:17:32 +0000 (22:17 +0100)]
Drop unneeded code to simplify getting hosts over ubus

This code in umdns_hosts was there since ever but it seems like an
unneeded copy & paste part of umdns_browse. In this function we iterate
over *host* entries of cached services list. It means the entry field
conains values like "example.local".

In such case looking for "._" is pointless and appending ".local"
results in looking for entries like "example.local.local".

This code make sense for browsing *services* only where for entries like
"ex._ssh._tcp.local" we try dumping "ex.local" & "ex._ssh._tcp.local".

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoDrop unneeded casting of "entry" from struct cache_service
Rafał Miłecki [Thu, 9 Mar 2017 13:01:27 +0000 (14:01 +0100)]
Drop unneeded casting of "entry" from struct cache_service

Since commit 819b909b82e4 ("cache: constify entry and host pointers in
struct cache_entry") it is "const char *" so we don't need to cast it.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoSet errno when refusing to send packet in interface_send_packet
Rafał Miłecki [Wed, 15 Feb 2017 10:37:57 +0000 (11:37 +0100)]
Set errno when refusing to send packet in interface_send_packet

Callers of interface_send_packet use perror and so expect errno to be
set in case of fail.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoFix error messages when sending packet fails
Rafał Miłecki [Wed, 15 Feb 2017 10:29:00 +0000 (11:29 +0100)]
Fix error messages when sending packet fails

Use perror in both cases, fix copy & paste mistake, don't use extra ":".

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoSend A in response to service discovery
Rafał Miłecki [Wed, 15 Feb 2017 10:17:04 +0000 (11:17 +0100)]
Send A in response to service discovery

This allows other mdns software to discover our hostname when they
start.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoUse A(AAA) when querying for domain on cache expire
Rafał Miłecki [Tue, 14 Feb 2017 12:55:46 +0000 (13:55 +0100)]
Use A(AAA) when querying for domain on cache expire

When cache entry is (halfly) expired we send query to get an update. So
far we were sending all queries using PTR records but for cached domains
it should be A(AAA) instead.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoDon't try sending unicast responses with multicast address
Rafał Miłecki [Tue, 14 Feb 2017 11:18:06 +0000 (12:18 +0100)]
Don't try sending unicast responses with multicast address

So far we were doing that when destination IP address wasn't specified.
Now we have all places in code cleaned up, stop accepting this and print
an error if needed.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoUse unicast IP address when sending unicast service reply
Rafał Miłecki [Tue, 14 Feb 2017 11:18:05 +0000 (12:18 +0100)]
Use unicast IP address when sending unicast service reply

For each protocol (IPv4 and IPv6) we have two interfaces (sockets): one
for unicast and one for multicast. If we noticed CLASS_UNICAST in the
multicast query we were switching to unicast interface for sending
reply.

The problem was not passing destination IP address. It was resulting in
sending packet to multicast IP using unicast interface. As we don't
set IP_MULTICAST_TTL / IPV6_MULTICAST_HOPS for unicast ones TTL was 1
and packets were ignored.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoPrepare support for sending unicast DNS answers
Rafał Miłecki [Tue, 14 Feb 2017 11:18:04 +0000 (12:18 +0100)]
Prepare support for sending unicast DNS answers

This adds extra argument to dns_send_answer & interface_send_packet
functions. For now we pass NULL-s only.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoPrint error if parsing service JSON fails
Rafał Miłecki [Wed, 15 Feb 2017 09:18:18 +0000 (10:18 +0100)]
Print error if parsing service JSON fails

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoSet TTL to 255 for outgoing IPv4 unicast packets
Rafał Miłecki [Mon, 13 Feb 2017 23:14:45 +0000 (00:14 +0100)]
Set TTL to 255 for outgoing IPv4 unicast packets

1) We SHOULD do this according to the RFC 6762 (Section 11, "Source
   Address Check").
2) We already do it for IPv6 unicast and both multicast interfaces
3) When receiving IPv4 packet we ignore it if TTL doesn't equal 255

In other words this is a requirement for working unicast communication
between 2 umdns instances (which doesn't seem to work yet though).

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoFix sending replies to PTR questions
Rafał Miłecki [Mon, 13 Feb 2017 15:42:35 +0000 (16:42 +0100)]
Fix sending replies to PTR questions

When we receive PTR question it includes hostname (instance), e.g.:
mdnsd: parse_question (391): Q -> PTR lede._http._tcp.local

First of all we should check if it matches hostname we use before trying
to reply. Secondly service_reply expects service with domain appended
(without hostname/instance) so we need to strip received string out of
hostname before passing it.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoAdd helper for checking if cached entry is for a host
Rafał Miłecki [Tue, 14 Feb 2017 12:31:07 +0000 (13:31 +0100)]
Add helper for checking if cached entry is for a host

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoDrop declaration of non-existing cache_lookup_name function
Rafał Miłecki [Tue, 14 Feb 2017 12:02:27 +0000 (13:02 +0100)]
Drop declaration of non-existing cache_lookup_name function

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoMake sdudp string const
Rafał Miłecki [Tue, 14 Feb 2017 11:59:10 +0000 (12:59 +0100)]
Make sdudp string const

It musn't be changed.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoRename cache scanning to updating
Rafał Miłecki [Fri, 10 Feb 2017 14:17:44 +0000 (15:17 +0100)]
Rename cache scanning to updating

What we were do is querying over all interfaces for all cached entries.
This isn't real scanning but rather updating (the cache).

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoSet TTL to the new value when updating cached service entry
Rafał Miłecki [Mon, 13 Feb 2017 07:43:27 +0000 (08:43 +0100)]
Set TTL to the new value when updating cached service entry

In the simplest case host may change TTL to some different value. It's
the most important for the goodbye packets though.

Consider situation when host (for which we have no no cached entries)
restarts. First it sends goodbye with TTL 0 (we cache it), then seconds
later it announces with standard TTL values (but we keep TTL 0).
It results in wrong cache state and could cause dropping valid entries.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoLoad service JSONs from /etc/umdns/
Rafał Miłecki [Sat, 11 Feb 2017 11:00:33 +0000 (12:00 +0100)]
Load service JSONs from /etc/umdns/

It matches recent project rename & uses directory that sholud be
persistent. Using /tmp/ could result in user having to setup JSONs after
every reboot.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoRename project to umdns
Rafał Miłecki [Fri, 10 Feb 2017 23:22:31 +0000 (00:22 +0100)]
Rename project to umdns

Using mdns name is a bit confusing as it's really close to Apple's mdnsd
binary name from their mDNSResponder project.
To make things worse OpenWrt's project was hosted in the mdnsd.git repo.

Rename this project to umdns which follows other projects naming schema
and is unique enough.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoDrop one-liner service_announce function
Rafał Miłecki [Fri, 10 Feb 2017 22:37:12 +0000 (23:37 +0100)]
Drop one-liner service_announce function

Now we have service_announce simplified so much there is no reason to
keep it as a helper.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoDon't use _services._dns-sd._tcp.local
Rafał Miłecki [Fri, 10 Feb 2017 22:37:11 +0000 (23:37 +0100)]
Don't use _services._dns-sd._tcp.local

It seems mdns(d) was trying to support queries for two records with
following names:
1) _services._dns-sd._udp.local
2) _services._dns-sd._tcp.local

According to the RFC 6763 Section 9 only the first one should be used
and response PTR records should include services of both protocols: UDP
and TCP.

This fixes discovering TCP services as in practice no other software was
sending queries with _services._dns-sd._tcp.local.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoSimplify UDP vs. TCP handling in service_announce_services
Rafał Miłecki [Fri, 10 Feb 2017 21:55:00 +0000 (22:55 +0100)]
Simplify UDP vs. TCP handling in service_announce_services

Out of 3 calls of this function only one doesn't have TCP vs. UDP
hardcoded. It's easier to move string check to that place and make this
function take "int tcp" argument instead.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoCheck correct attributes when loading service from blob
Rafał Miłecki [Fri, 10 Feb 2017 13:48:36 +0000 (14:48 +0100)]
Check correct attributes when loading service from blob

We access SERVICE_TXT so we should check for this entry. Checking
SERVICE_SERVICE doesn't make sense anyway as it's verified few lines
above (in the same funcion).

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoAvoid duplicated iteration over services when announcing
Rafał Miłecki [Fri, 10 Feb 2017 13:44:39 +0000 (14:44 +0100)]
Avoid duplicated iteration over services when announcing

In function service_announce_services we iterate over services and
handle every single one so calling service_reply which also iterates
doesn't make sense.

In simple cases it was just wasting CPU cycles. We got service, we were
passing its name and we were looking for it again comparing names.
In cases with one service available on few different ports it was worse.
For every iterated service we were announcing that one and all other
sharing the same name. It resulted in sending n^2 records.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoMove matching logic out of service_reply_single function
Rafał Miłecki [Fri, 10 Feb 2017 11:58:24 +0000 (12:58 +0100)]
Move matching logic out of service_reply_single function

There is only 1 place in project calling this function with the match
argument. It's easier to add prooper condition in this place and have
service_reply_single simplified by taking 1 less argument.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoSend A(AAA) records when announcing
Rafał Miłecki [Thu, 9 Feb 2017 09:11:16 +0000 (10:11 +0100)]
Send A(AAA) records when announcing

This allows device to be discovered by its local hostname.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoFix/change comment format about port 5353
Rafał Miłecki [Thu, 9 Feb 2017 08:19:05 +0000 (09:19 +0100)]
Fix/change comment format about port 5353

It wasn't ANSI C comment and there were 2 unneeded spaces.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years agoDon't call dns_reply_a from service_reply
Rafał Miłecki [Wed, 8 Feb 2017 17:04:27 +0000 (18:04 +0100)]
Don't call dns_reply_a from service_reply

This simplifies code without changing any behavior. Having this call in
service_reply required checking two conditions and was making code flow
harder to follow.

There are 2 more service_reply calls in the project:
1) In service_announce_services where we iterate over &services and
   every element of this list has "service" field filled. It means match
   argument was never NULL and dns_reply_a was never called from there.
2) In parse_question which also receives some name (there is a proper
   check in the dns_handle_packet). No call there neither.

So after all there was only 1 place that was indeed calling dns_reply_a.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoRename service_reply_a to dns_reply_a and move it to proper file
Rafał Miłecki [Wed, 8 Feb 2017 15:47:52 +0000 (16:47 +0100)]
Rename service_reply_a to dns_reply_a and move it to proper file

This function doesn't really do anything service specify, it just sends
an A(AAA) records. It could probably be used even without any services
registered.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoAdd simple "Fall through" comment to the announce_timer function switch
Rafał Miłecki [Wed, 8 Feb 2017 14:20:42 +0000 (15:20 +0100)]
Add simple "Fall through" comment to the announce_timer function switch

It's a common practice to add such comments to make it clear break
instruction was skipped on purpose.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agoChange dns_send_question function arg from unicast to multicast
Rafał Miłecki [Wed, 8 Feb 2017 13:04:57 +0000 (14:04 +0100)]
Change dns_send_question function arg from unicast to multicast

This trivial patch just reverses argument logic to make it a bit more
consistent with struct interface which contains "multicast" field. This
hopefully will make typos less likely and code easier to follow.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
7 years agodo not assume that dns structs are aligned
Johannes Wegener [Wed, 25 Jan 2017 23:07:00 +0000 (00:07 +0100)]
do not assume that dns structs are aligned

They do not have a fixed alignment in the packet. ARMv5 has separate
instructions for unaligned and aligned 16-bit load/store. The aligned
ones have undefined behavior if the addresses are unaligned, and that
completely breaks packet parsing.

Signed-off-by: Johannes Wegener <mail@johanneswegener.de>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agomdnsd: the service is ignoring questions
John Crispin [Mon, 19 Dec 2016 12:06:15 +0000 (13:06 +0100)]
mdnsd: the service is ignoring questions

When a question is incoming the service was incorrectly resettig the
query timeout regardless of it being the right service. This causes
the code to never answer the questions it is being asked.

Reported-by: Cristian Morales Vega <cristian@samknows.com>
Signed-off-by: John Crispin <john@phrozen.org>
7 years agocmake: Search for libjson-c
Florian Fainelli [Tue, 13 Dec 2016 22:23:53 +0000 (14:23 -0800)]
cmake: Search for libjson-c

Since we use libblobmsg_json which uses libjson-c internally, make sure
we find this library.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
7 years agoFix IPv6 read
Cristian Morales Vega [Wed, 14 Dec 2016 16:49:38 +0000 (16:49 +0000)]
Fix IPv6 read

This is a quick fix. It may still not work when the interface has
multiple IPs, and this also applies to IPv4.

Signed-off-by: Cristian Morales Vega <cristian@samknows.com>
8 years agoRevert "mdnsd: interface: enable looped back messages"
John Crispin [Mon, 24 Oct 2016 16:43:16 +0000 (18:43 +0200)]
Revert "mdnsd: interface: enable looped back messages"

This reverts commit 8a70b2b47dd328f8180e5ecaa7bdc817f574a81b.

8 years agomdnsd: interface: enable looped back messages
Eyal Birger [Sun, 22 May 2016 09:20:43 +0000 (12:20 +0300)]
mdnsd: interface: enable looped back messages

When the IP_MULTICAST_LOOP/IPV6_MULTICAST_LOOP socket options are not enabled,
locally generated queries are ignored by mdnsd; This prevents local
applications from being able to discover locally published services.

Signed-off-by: Eyal Birger <eyal.birger@gmail.com>
9 years agointerface: include libubox/utils.h
Felix Fietkau [Thu, 3 Sep 2015 12:59:12 +0000 (14:59 +0200)]
interface: include libubox/utils.h

Signed-off-by: Felix Fietkau <nbd@openwrt.org>